#include "Stack.h"

void STInit(ST* ps)
{
	assert(ps);
	ps->a = (STDataType*)malloc(sizeof(STDataType) * 5);
	if (ps->a == NULL)
	{
		perror("STLnit  malloc");
		return;
	}
	ps->top = 0;
	ps->capacity = 5;
}
void STDestory(ST* ps)
{
	assert(ps);
	free(ps->a);
	ps->a = NULL;
	ps->top = 0;
	ps->capacity = 0;
}
void Capacity(ST* ps)
{
	if (ps->capacity == ps->top)
	{
		STDataType* aaa = (STDataType*)realloc(ps->a, sizeof(STDataType) * ps->capacity * 2);
		if (aaa == NULL)
		{
			perror("Capacity  realloc");
			return;
		}
		ps->a = aaa;
		ps->capacity = 2 * ps->capacity;
	}
}
void STpush(ST* ps,STDataType x)
{
	assert(ps);
	Capacity(ps);
	ps->a[ps->top] = x;
	ps->top++;
}
void STpop(ST* ps)
{
	assert(ps);
	assert(STEmpty(ps));
	ps->top--;
}

//查栈顶元素
STDataType STTop(ST* ps)
{
	assert(ps);
	assert(STEmpty(ps));
	return ps->a[ps->top - 1];
}

//判断是否为空
bool STEmpty(ST* ps)
{
	assert(ps);
	if (ps->top == 0)
		return 0;
	else
		return 1;
}

//看个数
int STSize(ST* ps)
{
	assert(ps);
	return ps->top;
}